﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //var a =new {name='1',age=10 };
            //var b = new { cc="zz",aa=a};
            //Console.WriteLine(b);
            //a = null;
            //Console.WriteLine(b);

            //Console.Read();

            //StringBuilder sb=new StringBuilder();
            //Console.WriteLine(sb);
            //sb.Append("sssSelect * from  aaa  where 1=1");
            //sb.Append("  s    9");
            //sb.ToString().ToLower();
            //Console.WriteLine(sb.ToString().ToLower());


            string str = " SELECT * FROM (   SELECT LOG_A1_10,LOG_A1_20,LOG_A1_30,LOG_A1_40,LOG_A1_50,LOG_A1_60,  LOG_A1_70,LOG_A1_80,LOG_A1_90,LOG_A1_100,LOG_A1_110,LOG_A1_120,  LOG_A1_130,LOG_A1_140,LOG_A1_150,LOG_A1_160,LOG_A1_170,LOG_A1_180, LOG_A1_190,LOG_A1_200,LOG_A1_210,LOG_A1_220, A.BAS_A1_40 as logOperaTypeName,B.BAS_A1_40 as functionCodeName  FROM LOG_A1 left join BAS_A1 A on LOG_A1_20 = A.BAS_A1_30  left join BAS_A1 B on LOG_A1_50 = B.BAS_A1_30    WHERE   1=1  union    SELECT LOG_A2_10 as LOG_A1_10,LOG_A2_20 as LOG_A1_20,LOG_A2_30 as LOG_A1_30,  LOG_A2_40 as LOG_A1_40,'' as LOG_A1_50,'' as LOG_A1_60,  '' as LOG_A1_70,LOG_A2_50 as LOG_A1_80,   LOG_A2_60 as LOG_A1_90,'' as LOG_A1_100,'' as LOG_A1_110,'' as LOG_A1_120,  '' as LOG_A1_130,'' as LOG_A1_140,'' as LOG_A1_150,'' as LOG_A1_160,'' as LOG_A1_170, '' as LOG_A1_180,'' as LOG_A1_190,'' as LOG_A1_200,'' as LOG_A1_210, '' as LOG_A1_220,  A.BAS_A1_40 as logOperaTypeName,B.BAS_A1_40 as functionCodeName  FROM LOG_A2 left join BAS_A1 A on LOG_A2_20 = A.BAS_A1_30  left join BAS_A1 B on LOG_A2_50 = B.BAS_A1_30    WHERE   1=1  union    SELECT LOG_A3_10 as LOG_A1_10,LOG_A3_20 as LOG_A1_20,LOG_A3_30 as LOG_A1_30,  LOG_A3_40 as LOG_A1_40,LOG_A3_50 as LOG_A1_50,LOG_A3_60 as LOG_A1_60,  LOG_A3_70 as LOG_A1_70,LOG_A3_80 as LOG_A1_80 ,LOG_A3_90 as LOG_A1_90,   '' as LOG_A1_100,'' as LOG_A1_110,'' as LOG_A1_120,  '' as LOG_A1_130,'' as LOG_A1_140,'' as LOG_A1_150,'' as LOG_A1_160,'' as LOG_A1_170, '' as LOG_A1_180,'' as LOG_A1_190,'' as LOG_A1_200,'' as LOG_A1_210, '' as LOG_A1_220, A.BAS_A1_40 as logOperaTypeName,B.BAS_A1_40 as functionCodeName    FROM LOG_A3 left join BAS_A1 A on LOG_A3_20 = A.BAS_A1_30  left join BAS_A1 B on LOG_A3_50 = B.BAS_A1_30   WHERE   1=1  ) TEMPTABLE ORDER BY LOG_A1_80 DESC ";
            string result = "";
            foreach (var item in str.Split(' '))
            {
                result += item.Trim() + " ";
            }

            Console.WriteLine(DateTime.Now + " " + DateTime.Now.Millisecond);

            Console.WriteLine(remove1(str));
            Console.WriteLine(DateTime.Now + " " + DateTime.Now.Millisecond);
            Console.Read();


            //int[] sql = { 10, 20, 30, 35, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 520, 210, 220, 240, 250, 270, 280, 530, 300, 500, 400, 290, 560, 190, 200, 240, 230, 260, 310, 320, 330, 340, 350, 360, 370, 380, 390, 410, 420, 430, 440, 450, 460, 470, 480, 490, 510, 550, 570, 580, 590, 600, 610, 620, 630, 640, 650, 660, 670, 680, 690, 700, 710, 720, 730, 740, 750, 760, 770, 780, 790, 800, 810, 820, 830, 840, 850, 860, 870, 880, 890, 900, 910, 920, 930, 940, 100, 160, 540, 80, 80, 260 };
            //List<int> str = sql.ToList<int>();
            //str.Sort();
            //string result = "";
            //foreach (var item in str)
            //{
            //    if (!result.Contains(item.ToString()))
            //    {
            //        result += "ASM_A2_" + item+",";
            //    }
            //}
            //Console.WriteLine(result);
            //Console.Read();

            //string result = "";
            //for (int i = 1; i < 95; i++)
            //{
            //    result += "A.asm_a2_" + i + "0,";
            //}
            //Console.WriteLine(result);
        }
        static string remove(string str)
        {
            string result = "";
            string[] arr = str.Split(' ');
            int[] targetID = new int[10];
            int[] target7 = new int[10];
            int[] target8 = new int[10];
            int index = 0;
            string[] arr1 = new string[arr.Length];
            int arrindex = 0;
            for (int i = 0; i < arr.Length; i++)
            {
                if (!string.IsNullOrEmpty(arr[i]))
                {
                    arr1[arrindex] = arr[i];
                    arrindex++;
                }
            }
            for (int i = 0; i < arr1.Length; i++)
            {
                if (!string.IsNullOrEmpty(arr1[i]) && arr1[i].Contains("1=1"))
                {
                    targetID[index] = i;
                    target7[index] = i - 1;
                    target8[index] = i + 1;
                    arr1[i] = arr1[i].Replace("((1=1))", "");
                    arr1[i] = arr1[i].Replace("(1=1)", "");
                    arr1[i] = arr1[i].Replace("1=1", "");
                    index++;
                }
            }
            for (int i = 0; i < target8.Length; i++)
            {
                if (target8[i] > 0)
                {
                    //if ((string.IsNullOrEmpty(arr1[target8[i]]) && arr1[target7[i]].Trim().ToLower() == "where")||(!string.IsNullOrEmpty(arr1[target8[i]])&&arr1[target8[i]].Trim().ToLower()==")"&&arr1[target7[i]].Trim().ToLower() == "where"))
                    //{
                    //    arr1[target7[i]] = "";
                    //}
                    if (target8[i] < arr1.Length && !string.IsNullOrEmpty(arr1[target8[i]]) && arr1[target8[i]].Trim().ToLower() == "and")
                    {

                        arr1[target8[i]] = "";
                    }

                }

            }
            foreach (var item in arr1)
            {
                if (!string.IsNullOrEmpty(item))
                {
                    result += " " + item.Trim();
                }

            }


            return result;
        }


        static string remove1(string str)//消除1=1方法
        {
           
            //特殊1=1标准化（以后有什么不符合标准的1=1加到这里）
            str = str.Replace("( (1 = 1) )", "1=1");
            str = str.Replace("( 1 = 1 )", "1=1");
            str = str.Replace("(1 = 1)", "1=1");
            str = str.Replace("(1 =1)", "1=1");
            str = str.Replace("(1= 1)", "1=1");
            str = str.Replace("1 = 1", "1=1");
            if (!str.Contains("1=1"))//如果不存在  直接返回
            {
                return str;
            }
            string result = "";
            string[] arr = str.Split(' ');
            string[] arr1 = new string[arr.Length];
            int arrindex = 0;
            //消除多余空格
            for (int i = 0; i < arr.Length; i++)
            {
                if (!string.IsNullOrEmpty(arr[i]))
                {
                    arr1[arrindex] = arr[i];
                    arrindex++;
                }
            }
            //消除1=1和and（符合消除条件）
            for (int i = 0; i < arr1.Length; i++)
            {
                 if (!string.IsNullOrEmpty(arr1[i]) && arr1[i].Contains("1=1"))
                {
                    arr1[i] = arr1[i].Replace("((1=1))", "");
                    arr1[i] = arr1[i].Replace("(1=1)", "");
                    arr1[i] = arr1[i].Replace("1=1", "");
                    if (i + 1 < arr1.Length && !string.IsNullOrEmpty(arr1[i + 1]) && arr1[i + 1].Trim().ToLower() == "and")//1=1后面存在and 和其他条件
                    {
                        arr1[i + 1] = "";
                    }
                    #region 改进保留方法
                    //int andIndex = i - 1;//需要消除and 的 索引
                    //if (i + 1 < arr1.Length && !string.IsNullOrEmpty(arr1[i + 1]) && arr1[i + 1].Trim().ToLower() == "order")
                    //{
                    //    while (andIndex > 0 && string.IsNullOrEmpty(arr1[andIndex]))
                    //    {
                    //        andIndex--;
                    //    }
                    //    if (arr1[andIndex].Trim().ToLower() == "and")
                    //    {
                    //        arr1[andIndex] = "";
                    //    }
                    //}
                    #endregion
                }
            }
            foreach (var item in arr1)//将非空字符串拼装
            {
                if (!string.IsNullOrEmpty(item))
                {
                    result += " " + item.Trim();
                }
            }
            //消除where和and(符合消除条件)
            string[] resStr = result.Split(' ');
            for (int i = 0; i < resStr.Length; i++)
            {
                if (!string.IsNullOrEmpty(resStr[i]) && resStr[i].Trim().ToLower().Contains("where"))//消除遗留的where
                {
                    if (i + 1 >= resStr.Length)//where 后面 没有其他语句  消除
                    {
                        resStr[i] = "";
                    }
                    if (i + 1 < resStr.Length)//where 后面有其他语句， 是消除1=1以后 ） 或者 order 则 消除 where
                    {
                        if (!string.IsNullOrEmpty(resStr[i + 1]) && (resStr[i + 1].Trim().Substring(0,1) == ")" || resStr[i + 1].Trim().ToLower() == "order" || resStr[i + 1].Trim().ToLower() == "union"|| resStr[i + 1].Trim().ToLower() == "group"))
                        {
                            resStr[i] = "";
                        }
                    }
                }
                if (!string.IsNullOrEmpty(resStr[i]) && resStr[i].Trim().ToLower().Contains("and"))//消除遗留的and
                {
                    if (i + 1 >= resStr.Length)//and 后面 没有其他语句  消除
                    {
                        resStr[i] = "";
                    }
                    if (i + 1 < resStr.Length)//and 后面有其他语句， 是消除1=1以后 ） 或者 order和or group 则 消除 and
                    {
                        if (!string.IsNullOrEmpty(resStr[i + 1]) && (resStr[i + 1].Trim().Substring(0,1) == ")" || resStr[i + 1].Trim().ToLower() == "order" || resStr[i + 1].Trim().ToLower() == "or"|| resStr[i + 1].Trim().ToLower() == "group"))
                        {
                            resStr[i] = "";
                        }
                    }
                }

            }

            string fresult = "";
            foreach (var item in resStr)//将非空字符串拼装返回
            {
                if (!string.IsNullOrEmpty(item))
                {
                    fresult += " " + item;
                }
            }
            return fresult;
        }
    }
}